﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace HFUTCourseSimulation.Dialog {
    /// <summary>
    /// Interaction logic for EditCourse.xaml
    /// </summary>
    public partial class EditCourse : Window {
        public EditCourse() {
            InitializeComponent();
        }

        public Kernel.Data.Ui.Course CurrentCourse { get; set; }

        private void uiMainWindow_Loaded(object sender, RoutedEventArgs e) {
            this.DataContext = CurrentCourse;
        }

        private void uiMainWindow_Closed(object sender, EventArgs e) {
            this.DataContext = null;
        }

        private void uiSchedulesList_MouseDoubleClick(object sender, MouseButtonEventArgs e) {
            // It just an alias to edit
            uiCtxMenuEditSchedule_Click(sender, e);
        }

        private void uiCtxMenuNewSchedule_Click(object sender, RoutedEventArgs e) {
            // Create new item and order user edit it
            var item = new Kernel.Data.Storage.Schedule();
            var dialog = new EditSchedule();
            dialog.CurrentSchedule = new Kernel.Data.Ui.Schedule(item);
            dialog.ShowDialog();

            // Then insert or append it into list
            var idx = uiSchedulesList.SelectedIndex;
            if (idx < 0) {
                // No selection, append.
                CurrentCourse.Schedules.Add(dialog.CurrentSchedule);
            } else {
                // Has selection, insert
                CurrentCourse.Schedules.Insert(idx, dialog.CurrentSchedule);
            }
        }

        private void uiCtxMenuEditSchedule_Click(object sender, RoutedEventArgs e) {
            var idx = uiSchedulesList.SelectedIndex;
            if (idx < 0) return;

            var dialog = new EditSchedule();
            dialog.CurrentSchedule = CurrentCourse.Schedules[idx];
            dialog.ShowDialog();
        }

        private void uiCtxMenuDeleteSchedule_Click(object sender, RoutedEventArgs e) {
            // Check whether there is item can be deleted
            var idx = uiSchedulesList.SelectedIndex;
            if (idx < 0) return;

            // Order a confirm
            var rv = Util.Win32Dialog.Confirm("确认删除选中的课程安排吗？该操作不可撤销！", "确认删除");
            if (!rv) return;

            // Remove it
            CurrentCourse.Schedules.RemoveAt(idx);
        }

        private void uiCtxMenuClearSchedule_Click(object sender, RoutedEventArgs e) {
            // Order a confirm
            var rv = Util.Win32Dialog.Confirm("确认删除所有课程安排吗？该操作不可撤销！", "确认清空");
            if (!rv) return;

            // Clear all schedules
            CurrentCourse.Schedules.Clear();
        }

        private void uiColorPicker_MouseDown(object sender, MouseButtonEventArgs e) {
            var widget = new Widget.ColorPicker();
            widget.CurrentColor = CurrentCourse.Color;
            widget.ShowDialog();
        }
    }

}
